﻿import QtQuick 2.0
import QtQuick.Controls 2.12
import "./utils" as Utils

Rectangle {
    id: sceneRect
    property int currentSceneIndex: 0
    property bool allSceneOk: false

    signal startDrive()
    signal backIndex()

    color: "#1F2A37"
    border.width: 0
    
    // Rectangle{
    //     id: startDriveBtn
    //     x: 1088
    //     y: 29
    //     width: 160
    //     height: 55
    //     radius: 8
    //     enabled: false
    //     border.color: Qt.rgba(255, 255, 255, 0.5)
    //     color:  startDriveBtn.enabled ? "#2997FD" : "#20212C"
    //     Text {
    //         anchors.centerIn: parent
    //         text: "开始驾驶"
    //         font.family: "Microsoft YaHei"
    //         font.pixelSize: 24
    //         font.bold: true
    //         color: startDriveBtn.enabled ? "white" : "#4D4D56"
    //     }
    //     MouseArea{
    //         anchors.fill: parent
    //         onClicked: {
    //             mingDiPopup.open()
    //         }
    //     }
    // }

    Row{
        x: 636
        y: 322
        spacing: 48
        Utils.MyCheckBtn{
            id: checkNotOk
            btnNameColor: "#FFFFFF"
            btnNameText: "不安全"
            pressColor: "#F05252"
            onChecked: {
                checkOk.isChecked = false
                sceneRepeater.itemAt(sceneRect.currentSceneIndex).preCheckState = 1
                sceneRepeater.itemAt(sceneRect.currentSceneIndex).checkState = 0
                sceneRepeater.itemAt(sceneRect.currentSceneIndex).checkState = 2
                // startDriveBtn.enabled = sceneRect.updateStateIndex()
                mySecenesPopup.is_ok = false
                mySecenesPopup.open();
            }
        }

        Utils.MyCheckBtn{
            id: checkOk
            btnNameColor: "#FFFFFF"
            btnNameText: "安全"
            pressColor: "#00D160"
            onChecked: {
                checkNotOk.isChecked = false
                sceneRepeater.itemAt(sceneRect.currentSceneIndex).preCheckState = 2
                sceneRepeater.itemAt(sceneRect.currentSceneIndex).checkState = 2
                if(sceneRect.currentSceneIndex < 3)
                {
                    sceneRect.currentSceneIndex = sceneRect.currentSceneIndex + 1;
                    image_manager.view_state = sceneRect.currentSceneIndex + 1 
                    checkOk.isChecked = sceneRepeater.itemAt(sceneRect.currentSceneIndex).preCheckState==2 ? true : false
                    checkNotOk.isChecked = sceneRepeater.itemAt(sceneRect.currentSceneIndex).preCheckState==1 ? true : false
                    sceneRepeater.itemAt(sceneRect.currentSceneIndex).checkState = 1;
                    for(var idx = 0;idx < sceneRepeater.model.length;idx++)
                    {
                        if(idx !== index && sceneRepeater.itemAt(idx).checkState === 1)
                        {
                            if(sceneRepeater.itemAt(idx).preCheckState !== 0)
                            {
                                sceneRepeater.itemAt(idx).checkState = 2;
                            }
                            else{
                                sceneRepeater.itemAt(idx).checkState = 0;
                            }
                        }
                    }
                }
                var idx = 0
                for(;idx < sceneRepeater.model.length;idx++)
                {
                    if(sceneRepeater.itemAt(idx).checkState !== 2 || sceneRepeater.itemAt(idx).preCheckState !== 2)
                    {
                        break;
                    }
                }
                if(idx >= sceneRepeater.model.length)
                {
                    mySecenesPopup.is_ok = true
                    mySecenesPopup.open();
                }
                // startDriveBtn.enabled = sceneRect.updateStateIndex()
            }
        }
    }
    Utils.MySecenesPopup{
        id:mySecenesPopup
        is_ok:true
        x:550
        y:123
        Component.onCompleted: {
            // mySecenesPopup.open()
        }        
    }
    Rectangle{
        color: "transparent"
        visible: mySecenesPopup.visible
        Image {
            width:442
            height:442
            source: mySecenesPopup.is_ok ? "qrc:/scraper/imgs/around_ok_view.png":"qrc:/scraper/imgs/around_nook_view.png"
            x: 80
            y: 54
            z: 2
        }
    }
    Utils.MyDriverInfo{
        anchors.bottom: parent.bottom
        visible: swipeView.currentIndex === 2
        view_model: 1
    }


    Image{
        id: vehicleImage
        x: 258
        y: 104
        height: 344
        width: 88
        z: 1
        fillMode: Image.PreserveAspectFit
        asynchronous: true
        source: "qrc:/scraper/imgs/scraper.png"
    }

    ListModel{
        id: loaderSceneSources
        ListElement {
            width:260
            height:120
            source: "qrc:/scraper/imgs/front_view.png"
            x: 172
            y: 48
            z: 2
        }
        ListElement {
            width:242
            height:120
            source: "qrc:/scraper/imgs/back_view.png"
            x: 183
            y:398
            z: 2
        }
        ListElement {
            width:144
            height:315
            source: "qrc:/scraper/imgs/left_view.png"
            x: 144
            y: 126  
            z: 2
        }
        ListElement {
            width:144
            height:315
            source: "qrc:/scraper/imgs/right_view.png"
            x: 318
            y: 126
            z: 2
        }
        ListElement {
            source: "qrc:/scraper/imgs/dao_back_view.png"
            x: -77
            y: 121
            z: 0
        }
        ListElement {
            source: "qrc:/scraper/imgs/around_view.png"
            x: 92
            y: 121
            z: 0
        }
    }

    Image{
        id: sceneImage
        fillMode: Image.PreserveAspectFit
        visible: !mySecenesPopup.visible
        x: loaderSceneSources.get(sceneRect.currentSceneIndex).x
        y: loaderSceneSources.get(sceneRect.currentSceneIndex).y
        z: loaderSceneSources.get(sceneRect.currentSceneIndex).z
        width: loaderSceneSources.get(sceneRect.currentSceneIndex).width
        height: loaderSceneSources.get(sceneRect.currentSceneIndex).height
        source: loaderSceneSources.get(sceneRect.currentSceneIndex).source
    }

    Row{
        x: 142
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 136
        spacing: 12
        Repeater{ 
            id: sceneRepeater
            model: ["前视野", "后视野", "左视野", "右视野"]

            delegate: Utils.MySceneCheckItem{
                id: sceneCheckItem
                sceneName: modelData
                
                Component.onCompleted: {
                    if(sceneRect.currentSceneIndex === index)
                    {
                        checkState = 1
                    }
                }
                
                onSceneSelect:{
                    mySecenesPopup.close()
                    sceneRect.currentSceneIndex = index;
                    image_manager.view_state = index + 1
                    checkOk.isChecked = preCheckState==2 ? true : false
                    checkNotOk.isChecked = preCheckState==1 ? true : false
                    sceneCheckItem.checkState = 1;
                    for(var idx = 0;idx < sceneRepeater.model.length;idx++)
                    {
                        if(idx !== index && sceneRepeater.itemAt(idx).checkState === 1)
                        {
                            if(sceneRepeater.itemAt(idx).preCheckState !== 0)
                            {
                                sceneRepeater.itemAt(idx).checkState = 2;
                            }
                            else{
                                sceneRepeater.itemAt(idx).checkState = 0;
                            }
                        }
                    }
                }

                Connections{
                    target: sceneRect
                    function onCurrentSceneIndexChanged(){
                        if(sceneRect.currentSceneIndex == index){
                            sceneCheckItem.checkState = 1
                        }
                        else{
                            if(sceneCheckItem.preCheckState == 0){
                                sceneCheckItem.checkState = 0
                            }else{
                                sceneCheckItem.checkState = 2
                            }
                        }
                    }
                }
            }

            onModelChanged:{
                if(sceneRepeater.count >0)
                    sceneRepeater.itemAt(0).checkState = 1
            }
        }
    }
    Rectangle{
        color: "transparent"
        width: 559
        height: 128
        y: 114
        x:604
        Column{
            spacing: 8
            Text{
                color: "#F1F1F1"
                text: "请确认右视野范围是否安全？"
                font.family: "Microsoft YaHei"
                font.pixelSize: 40
            }
            Text{
                color: "#8E98A8"
                text:"请仔细观察右视野图像中是否有障碍物？"
                font.family: "Microsoft YaHei"
                font.pixelSize: 28
            }
        }
    }


    // Utils.MyPopup{
    //     parent: parent
    //     id:myPopup
    //     onClose_All: {
    //         backIndex()
    //         reClear()
    //     }
    // }

    // Image {
    //     x: 1205
    //     y: 688
    //     source: "qrc:/imgs/back.png"
    //     MouseArea{
    //         anchors.fill: parent
    //         onClicked: {
    //             myPopup.open()
    //         }
    //     }
    // }

    Popup{
        id: mingDiPopup
        width: 634
        height: 338
        closePolicy: Popup.CloseOnEscape
        anchors.centerIn: parent
        background: Rectangle{
            color: "#252A39"
            border.color: "#20FFFFFF"
            border.width: 1
            radius: 8
        }

        Text{
            y: 100
            anchors.horizontalCenter: parent.horizontalCenter
            text: "请鸣笛"
            color: "white"
            font{
                family: "Microsoft YaHei"
                pixelSize: 28
            }
        }

        Rectangle{
            id: okBtn
            y: 200
            width: 193
            height: 67
            anchors.horizontalCenter: parent.horizontalCenter
            radius: 8
            border.width: 0
            color: "dodgerblue"
            Text{
                id: connectText
                anchors.centerIn: parent
                text: "确定"
                color: "white"
                font{
                    family: "Microsoft YaHei"
                    pixelSize: 28
                }
            }
            MouseArea{
                anchors.fill: parent
                onClicked: {
                    recheckTimer.stop()
                    startDrive()
                    reClear()
                    mingDiPopup.close()
                }
            }
        }
    }

    Timer {
        id: recheckTimer
        interval: 60000 * 10;  // 10分钟未驾驶，需重新检查
        running: false;
        repeat: true
        onTriggered: {
            reClear()
        }
    }

    function reClear(){
        for(var i=0; i < sceneRepeater.count; i++){
            sceneRepeater.itemAt(i).checkState = 0
            sceneRepeater.itemAt(i).preCheckState = 0
        }
        sceneRepeater.itemAt(0).checkState = 1
        checkOk.isChecked = false
        checkNotOk.isChecked = false
        // startDriveBtn.enabled = false
        sceneRect.currentSceneIndex = 0
    }

    function updateStateIndex(){
        var ok_flag = true
        for(var i=0; i < sceneRepeater.count; i++){
            var item = sceneRepeater.itemAt(i)
            if(item.preCheckState !== 2 ){
                ok_flag = false
            }

            if(item.checkState === 0){
                checkOk.isChecked = false
                checkNotOk.isChecked = false
                sceneRect.currentSceneIndex = i
                return false
            }
        }
        return ok_flag
    }

}
